import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:goumao_app/Customs/my_button.dart';
import 'package:goumao_app/Customs/my_tag_view.dart';
import 'package:goumao_app/Model/gm_home_model.dart';

import '../Customs/gm_app_util.dart';
import 'gm_chat_ui.dart';

class GMHomeDetailPage extends StatefulWidget {
  final GMHomeData homeData;

  const GMHomeDetailPage({super.key, required this.homeData});

  @override
  // ignore: library_private_types_in_public_api
  _GMHomeDetailPageState createState() => _GMHomeDetailPageState();
}

class _GMHomeDetailPageState extends State<GMHomeDetailPage> {
  Widget _buildCarouselItem(String imageUrl) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: NetworkImage(imageUrl),
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  // ignore: non_constant_identifier_names
  Widget GMConastInfo(String imgPath, String text, VoidCallback onPressed) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        GMMyButton(
            direction: GMMyButtonDirection.left,
            text: text,
            image: Image.asset(
              imgPath,
              width: 20,
              height: 20,
            )),
        TextButton(
          onPressed: onPressed,
          style: TextButton.styleFrom(
            backgroundColor: const Color(0xffcccccc), // 按钮背景颜色
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(4.0), // 按钮圆角
            ),
          ),
          child: const Text(
            '复制',
            style: TextStyle(color: Color(0xff555555), fontSize: 14),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("领养详情"),
        elevation: 0,
        actions: [
          IconButton(
            icon: Image.asset(
              "assets/images/gm_xq_gd_btn.png",
              width: 25,
            ),
            onPressed: () {
              GMAppUtil.popFromBottomView(context, "", [
                ListItem(
                    text: '举报',
                    onTap: () => {
                          Navigator.of(context).pop(),
                          GMAppUtil.userReport(context)
                        }),
                ListItem(text: '拉黑', onTap: () => {Navigator.of(context).pop()})
              ]);
            },
          ),
        ],
      ),
      body: SafeArea(
        child: Column(
          children: [
            // 可滑动的页面
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    /// 顶部轮播图
                    CarouselSlider(
                      options: CarouselOptions(
                        height: MediaQuery.of(context).size.width,
                        autoPlay: true,
                        viewportFraction: 1.0,
                        enlargeCenterPage: false,
                      ),
                      items: widget.homeData.files!.map((GMHomeFiles file) {
                        return _buildCarouselItem(
                            "https://file.onepetmall.com/${file.url}");
                      }).toList(),
                    ),
                    Container(
                      padding: const EdgeInsets.fromLTRB(15, 25, 15, 16),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Container(
                                height: 20,
                                padding: const EdgeInsets.only(
                                    right: 8, left: 8, top: 1, bottom: 1),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(2),
                                  color: const Color(0xffFFE8E8),
                                ),
                                child: Text(
                                  widget.homeData.source!,
                                  style: const TextStyle(
                                      color: Color(0xffFF8386),
                                      fontSize: 12,
                                      fontWeight: FontWeight.w300),
                                ),
                              ),
                              const SizedBox(
                                width: 10,
                              ),
                              Text(
                                widget.homeData.nick!,
                                style: const TextStyle(
                                    color: Color(0xff333333),
                                    fontSize: 20,
                                    fontWeight: FontWeight.w700),
                              )
                            ],
                          ),
                          const SizedBox(
                            height: 20,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                  child: SizedBox(
                                child: Row(
                                  children: [
                                    Image.asset(
                                      "assets/images/gm_wz_icon.png",
                                      width: 18,
                                      height: 18,
                                    ),
                                    const SizedBox(
                                      width: 4,
                                    ),
                                    Expanded(
                                        child: Text(
                                      widget.homeData.locationValue(),
                                      style: const TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 14),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ))
                                  ],
                                ),
                              )),
                              Text(
                                widget.homeData.publicTimeValue(),
                                style: const TextStyle(
                                    color: Color(0xff666666), fontSize: 14),
                              )
                            ],
                          ),
                          const SizedBox(
                            height: 25,
                          ),
                          Container(
                            color: const Color(0xffcccccc),
                            height: 0.5,
                          ),
                          const SizedBox(
                            height: 16,
                          ),
                          const Text(
                            "TA的故事",
                            style: TextStyle(
                                color: Color(0xff333333),
                                fontSize: 20,
                                fontWeight: FontWeight.w700),
                          ),
                          const SizedBox(
                            height: 12,
                          ),
                          Text(
                            widget.homeData.story!,
                            style: const TextStyle(
                                color: Color(0xff666666), fontSize: 14),
                          ),
                          const SizedBox(
                            height: 20,
                          ),
                          widget.homeData.huiFangTags().length == 2
                              ? Row(
                                  children: [
                                    GMTagView(
                                      text: widget.homeData.huiFangTags().first,
                                      backgroundColor: const Color(0xffF7F8FC),
                                      borderRadius: 2,
                                      textStyle: const TextStyle(
                                          color: Color(0xffFFA54A),
                                          fontSize: 10),
                                    ),
                                    GMTagView(
                                      text: widget.homeData.huiFangTags()[1],
                                      backgroundColor: const Color(0xffF7F8FC),
                                      borderRadius: 2,
                                      textStyle: const TextStyle(
                                          color: Color(0xffFFA54A),
                                          fontSize: 10),
                                    )
                                  ],
                                )
                              : const Text(
                                  "送养人信息",
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: 20,
                                      fontWeight: FontWeight.w700),
                                ),
                          // const SizedBox(
                          //   height: 25,
                          // ),
                          Column(
                            children: [
                              GMConastInfo("assets/images/gm_wx_icon.png",
                                  widget.homeData.wechat!, () {}),
                              Visibility(
                                visible: widget.homeData.phone!.isNotEmpty,
                                child: GMConastInfo(
                                    "assets/images/gm_sj_icon.png",
                                    widget.homeData.phone!,
                                    () {}),
                              ),
                            ],
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
            // 底部固定高度的 Container
            Container(
              height: 74.0,
              padding: const EdgeInsets.fromLTRB(33, 0, 33, 20),
              child: InkWell(
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => GMChatPage(
                        homeData: widget.homeData,
                        title: widget.homeData.nick,
                      ),
                    ),
                  );
                },
                child: Container(
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(25),
                      color: const Color(0xffFF6A6E)),
                  child: const Center(
                    child: Text(
                      "咨询详情",
                      style: TextStyle(
                          color: Color(0xffffffff),
                          fontSize: 15,
                          fontWeight: FontWeight.w400),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
